package com.foreveross.dao.imp;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.foreveross.common.SQLUtil;
import com.foreveross.dao.ProductTypeDAO;
import com.foreveross.vo.Log;
import com.foreveross.vo.ProductType;

@Repository("productTypeDAO")
public class ProductTypeDAOImp extends SQLUtil implements ProductTypeDAO {

	@Override
	public List<ProductType> findProductTypeList(String proCode,String pro_name,
			String proLineName, String pageIndex, String pageSize) throws Exception {
		StringBuffer queryString = new StringBuffer(
				"select model from ProductType model where 1=1 ");
		if (!"".equals(proCode) && null != proCode) {
			queryString.append(" and model.id ='" + proCode + "'");
		}
		if (!"".equals(pro_name) && null != pro_name) {
			queryString.append(" and model.proName  like '%" + pro_name + "%'");
		}
		if (!"".equals(proLineName) && null != proLineName) {
			queryString.append(" and model.proLineName like '%" + proLineName
					+ "%'");
		}
		queryString.append(" order by model.id desc");
		return (List<ProductType>) findObjectStrAll(queryString.toString(),
				pageIndex, pageSize);
	}

	@Override
	public long findProductTypeListCount(String proCode,String pro_name, String proLineName)
			throws Exception {
		StringBuffer queryString = new StringBuffer(
				"select count(*) from ProductType model where 1=1 ");
		if (!"".equals(proCode) && null != proCode) {
			queryString.append(" and model.proCode = '" + proCode + "'");
		}
		if (!"".equals(pro_name) && null != pro_name) {
			queryString.append(" and model.proName  like '%" + pro_name + "%'");
		}
		if (!"".equals(proLineName) && null != proLineName) {
			queryString.append(" and model.proLineName like '%" + proLineName
					+ "%'");
		}
		return (Long) findObjectAll(queryString.toString()).get(0);
	}

	@Override
	public List<Object[]> findProductTypeSelect()
			throws Exception {
		StringBuffer queryString = new StringBuffer();
		queryString.append(" select distinct model.id, model.proCode,model.proName from ProductType model ");
		queryString.append(" group by model.proCode ");
		queryString.append(" order by model.id desc");
		return (List<Object[]>) findObjectStrAll(queryString.toString());
	}
	
	@Override
	public List<Object[]> findProductTypeLineSelect(String proCode) throws Exception {
		StringBuffer queryString = new StringBuffer();
		queryString.append(" select distinct model.id, model.proLineCode,model.proLineName from ProductType model  where 1=1 ");
		
		if (!"".equals(proCode) && null != proCode) {
			queryString.append(" and model.proCode = '" + proCode + "'");
		}
		queryString.append(" group by model.proLineCode ");
		queryString.append(" order by model.id desc");
		return (List<Object[]>) findObjectStrAll(queryString.toString());
	}
	
	@Override
	public void deleteProductType(String productTypeId) throws Exception {
		 final String sql = "delete from ProductType t where t.id='" + productTypeId + "'";
		 updateObejctByHql(sql); 
	}


}
